#!/usr/bin/env python
# -*- coding: utf-8 -*-

from google.appengine.ext import db

import data.model
import module.relatedkeyword
import logging

NOT_SPECIFIC_PROVIDER = 10000

class SearchProducts:
    @staticmethod
    def related_search(keyword, area, providerid, start_offset, limit, expired):
        related_keywords = module.relatedkeyword.RelatedKeyword.get(keyword)
        related_keywords.insert(0, keyword)
        
        retData = []
        bEnd = False
        
        local_start_offset = 0
        local_count = 0
        raw = 0
        for word in related_keywords:
            results = SearchProducts.exact_search(word.encode('utf-8').decode('utf-8'), area, providerid, 0, 100, expired)
            
            for result in results:
                logging.info("raw count:" + str(raw))
                logging.info("local_start_offset" + str(local_start_offset))
                logging.info("start_offset" + str(start_offset))
                
                raw+=1
                
                if bEnd:
                    break
                if local_start_offset < start_offset:
                    local_start_offset+=1
                    continue
                    
                retData.append(result)
                local_count+=1
                if local_count >= limit:
                    bEnd = True
            if bEnd:
                break
        return retData, related_keywords
    
    @staticmethod
    def exact_search(keyword, area, providerid, start_offset, limit, expired):
        q = SearchProducts.queryBuinder(keyword, area, providerid, expired)
        return q.fetch(limit, start_offset)
    
    @staticmethod
    def queryBuinder(keyword, area, providerid, expired):
        if(keyword!="*"):
            keyword_end = keyword + u"\ufffd"
            if(area!="*"):
                if(providerid!=NOT_SPECIFIC_PROVIDER):
                    if(expired == 1):
                        logging.info("#1")
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND area = :3 AND social_provider_code = :4", 
                        keyword, keyword_end, area, providerid)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND area = :3 AND social_provider_code = :4 AND is_expired = 0", 
                        keyword, keyword_end, area, providerid)
                else:   # provider is *
                    if(expired == 1):
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND area = :3", 
                        keyword, keyword_end, area)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND area = :3 AND is_expired = 0", 
                        keyword, keyword_end, area)
            else: # area is *
                if(providerid!=NOT_SPECIFIC_PROVIDER):
                    if(expired == 1):
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND social_provider_code = :3", 
                        keyword, keyword_end, providerid)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND social_provider_code = :3 AND is_expired = 0", 
                        keyword, keyword_end, providerid)
                else:   # provider is *
                    if(expired == 1):
                        logging.info("#5")
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2", 
                        keyword, keyword_end)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE keywords >= :1 AND keywords < :2 \
                                            AND is_expired = 0", 
                        keyword, keyword_end)
        else: # keyword is *
            if(area!="*"):
                if(providerid!=NOT_SPECIFIC_PROVIDER):
                    if(expired == 1):
                        q = db.GqlQuery("SELECT * FROM Products WHERE area = :1 AND social_provider_code = :2", 
                        area, providerid)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE area = :1 AND social_provider_code = :2 AND is_expired = 0", 
                        area, providerid)
                else:   # provider is *
                    if(expired == 1):
                        q = db.GqlQuery("SELECT * FROM Products WHERE area = :1", area)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE area = :1 AND is_expired = 0", area)
            else: # area is *
                if(providerid!=NOT_SPECIFIC_PROVIDER):
                    if(expired == 1):
                        q = db.GqlQuery("SELECT * FROM Products WHERE social_provider_code = :1", providerid)
                    else:
                        q = db.GqlQuery("SELECT * FROM Products WHERE social_provider_code = :1 AND is_expired = 0", providerid)
                else:   # provider is *
                    if(expired == 1):
                        logging.info("#2")
                        q = db.GqlQuery("SELECT * FROM Products ORDER BY sell_delta DESC")
                    else:
                        logging.info("#3")
                        q = db.GqlQuery("SELECT * FROM Products WHERE is_expired = 0 ORDER BY sell_delta DESC")
        return q                
